/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    $.ajax({
        type: apis.doctorIndex.method,
        url: apis.doctorIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//HTML
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].realname + '</td>';
        if (d[i].head = '') {
            h += '<td><img src="' + d[i].head + '" height="50"/></td>';
        } else {
            h += '<td>-</td>';
        }
        h += '<td>' + d[i].work_type_name + '</td>';
        h += '<td>' + d[i].work_title_name + '</td>';
        h += '<td>' + d[i].hospital_name + '</td>';
        h += '<td>' + d[i].depart_name + '</td>';
        h += '<td>' + d[i].is_apply_name + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        if (d[i].is_apply == 3) {
            h += '-';
        } else {
            h += '<a href="javascript:;" onclick="auth_ok(' + d[i].id + ');" style="color: green;">';
            h += '通过';
            h += '</a>';
            h += ' &nbsp; ';
            h += '<a href="javascript:;" onclick="auth_no(' + d[i].id + ');" style="color: red;">';
            h += '驳回';
            h += '</a>';
        }
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_daiban(\'代办列表\',\'doctor-daiban-list.html\',' + d[i].uid + ');">';
        h += '代办列表';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_yihu(\'小区医护列表\',\'doctor-yihu-list.html\',' + d[i].uid + ');">';
        h += '小区医护';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_peizhen(\'陪诊列表\',\'doctor-peizhen-list.html\',' + d[i].uid + ');">';
        h += '陪诊列表';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//审核通过
function auth_ok(v){
    var msg = '确认要 <b style="color: green; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 2});
    });
}
//审核拒绝
function auth_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">拒绝</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 1});
    });
}
//审核API
function authApi(e){
    $.ajax({
        type: apis.doctorAuth.method,
        url: apis.doctorAuth.url,
        data: { id: e.id, auth: e.auth },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('操作成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//代办列表弹窗
function lay_daiban(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//代办列表
function daibanList(v){
    $.ajax({
        type: apis.doctorDaibanList.method,
        url: apis.doctorDaibanList.url,
        data: { uid: v },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td>没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].yiyuan_name + '</td>';
                h += '<td>' + d[i].yytime + '</td>';
                h += '<td>' + d[i].remark + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td class="f-14">';
                if (d[i].status == 1) {
                    h += '<a href="javascript:;" onclick="lay_daiban_auth(' + d[i].id + ');">';
                    h += '设置已处理';
                    h += '</a>';
                } else {
                    h += '-';
                }
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//设置已处理
function lay_daiban_auth(v){
    var msg = '确认要 <b style="font-size: 20px;">设置已处理</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.doctorDaibanModify.method,
            url: apis.doctorDaibanModify.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('操作成功!');
                setTimeout(function(){
                    //top.location.reload();
                    location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//医护列表弹窗
function lay_yihu(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//医护列表
function yihuList(v){
    $.ajax({
        type: apis.doctorYihuList.method,
        url: apis.doctorYihuList.url,
        data: { uid: v },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td>没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].yytime + '</td>';
                h += '<td>' + d[i].yydizhi + '</td>';
                h += '<td>' + d[i].remark + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//陪诊列表弹窗
function lay_peizhen(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//陪诊列表
function peizhenList(v){
    $.ajax({
        type: apis.doctorPeizhenList.method,
        url: apis.doctorPeizhenList.url,
        data: { uid: v },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td>没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].pat_name + '</td>';
                h += '<td>' + d[i].pat_phone + '</td>';
                h += '<td>' + d[i].yytime + '</td>';
                h += '<td>' + d[i].yiyuan_name + '</td>';
                h += '<td>' + d[i].delay + '</td>';
                h += '<td>' + d[i].remark + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}